package labox.innovation.gameserver.network.gameserverpackets;

import java.io.IOException;
import java.security.GeneralSecurityException;
import java.security.interfaces.RSAPublicKey;

import javax.crypto.Cipher;

import labox.innovation.config.Config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author -Wooden-
 * 
 */
public class BlowFishKey extends GameServerBasePacket {
	private static final Logger _log = LoggerFactory.getLogger(BlowFishKey.class.getName());

	/**
	 * @param blowfishKey
	 * @param publicKey
	 */
	public BlowFishKey(byte[] blowfishKey, RSAPublicKey publicKey) {
		writeByte(0x00);
		byte[] encrypted = null;
		try {
			Cipher rsaCipher = Cipher.getInstance("RSA/ECB/nopadding");
			rsaCipher.init(Cipher.ENCRYPT_MODE, publicKey);
			encrypted = rsaCipher.doFinal(blowfishKey);
		} catch (GeneralSecurityException e) {
			_log.error("Error While encrypting blowfish key for transmision (Crypt error)", Config.SERVER_ID, e);
		}
		writeInt(encrypted.length);
		writeBytes(encrypted);
	}

	/* (non-Javadoc)
	 * @see net.sf.l2j.gameserver.gameserverpackets.GameServerBasePacket#getContent()
	 */
	@Override
	public byte[] getContent() throws IOException {
		return getBytes();
	}

}
